package com.fast.security.service;

import com.fast.security.user.JwtUserDetails;
import com.fast.service.service.IUserService;
import lombok.AllArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Map;

/**
 * Class UserDetailsServiceImpl
 *
 * @Auther: lhy
 * @Date: 2021/12/7 11:11
 * @Mail: 190643201@qq.com
 */
@Service("userDetailsService")
@AllArgsConstructor
public class UserDetailsServiceImpl implements UserDetailsService {


    private final IUserService iUserService;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        ArrayList<GrantedAuthority> authorities = new ArrayList<>();
        authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
        Map<String, Object> user = iUserService.select().eq("mobile", s).end().find();
        return new JwtUserDetails(s, user.get("password").toString(), authorities);
    }
}
